<?php
namespace app\common\model;
use think\Model;
use think\model\concern\SoftDelete;
class Admin extends Model{
 

 //软删除
 use SoftDelete;
 

 //登录校验
 
 public function login($data){
 	$validate=new \app\common\validate\Admin();
 	if(!$validate->scene('login')->check($data)){

 		return $validate->getError();
 	}
 	$result=$this->where($data)->find();
     if($result){ 
     	//检测账户是否可用
     	if($result['status']!=1){
     		return "此账户当前不可用！";
     	}
     	$sessionData=[
     		'id'=>$result['id'],
     		'nickname'=>$result['nickname'],
     		'is_super'=>$result['is_super']

     	];
     	session('admin',$sessionData);
     	return 1;
     }else{
     	return "用户名或密码错误！";
     }
 }

 //注册校验 
 public function register($data){
     $validate=new \app\common\validate\Admin();
     if(!$validate->scene('register')->check($data)){

          return $validate->getError();
     }
          $result=$this->allowField(true)->save($data);
          if($result){
               //发送邮件
               mailto($data['email'],'注册成功！','验证码为：123456');
               return 1;
          }else{
               return "注册失败！";
          }
}
//重置密码
 public function reset($data){
     $validate=new \app\common\validate\Admin();
     if(!$validate->scene('reset')->check($data)){
          return $validate->getError();
     }
     if($data['code']!=session('code')){
          return '验证码不正确';
     }
     $adminInfo=$this->where('email',$data['email'])->find();
     $newpassword=$data['newpassword'];
     $adminInfo->password=$newpassword;
     $result=$adminInfo->save();
     if($result){
          $content=
          '重置密码成功<br>账户名为：'.$adminInfo['username'].'<br>'.'新密码为：'.$newpassword;
          mailto($data['email'],'重置密码成功',$content);
          return 1;
     }else{
          return '重置密码失败';
     }
     }
     //编辑管理员
     public function edit($data){
          $validate= new \app\common\validate\Admin();
          if(!$validate->scene('edit')->check($data)){
          return $validate->getError();
     }
     $adminInfo=$this->find($data['id']);
     $adminInfo->status=$data['status'];
     $adminInfo->is_super=$data['is_super'];
     $adminInfo->nickname=$data['nickname'];
     $adminInfo->email=$data['email'];
     $result=$adminInfo->save();
     if($result){
          return 1;
     }else{
          return '修改失败！';
     }
     }



}

